package com.wjw.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wjw.pojo.Book;
import com.wjw.pojo.Type;
import com.wjw.service.BookSersive;
import com.wjw.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("type")
public class TypeController {
    @Autowired
    private TypeService typeService;
    @Autowired
    private BookSersive bookService;

    @RequestMapping("listType") //分页,模糊查询
    public String list(@RequestParam(required = false, defaultValue = "1", value = "pageNum") Integer pageNum,
                       @RequestParam(required = false, defaultValue = "10", value = "pageSize") Integer pageSize, Model model, Type type) {
        if (pageNum <= 0 || pageNum.equals("") || pageNum == null) {
            pageNum = 1;
        }
        if (pageSize <= 0 || pageSize.equals("") || pageSize == null) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<Type> qw = new QueryWrapper<>();
        if (type.getType() != null) {
            qw.like("type", type.getType());
        }

        List<Type> list = typeService.list(qw);
        PageInfo<Type> objectPageInfo = new PageInfo<Type>(list);
        model.addAttribute("pageInfo", objectPageInfo);
        return "typeList";
    }

    //跳转页面,准备添加
    @RequestMapping("preSaveType")
    public String preSaveType() {
        return "typeSave";
    }

    //添加类型
    @RequestMapping("saveType")
    public String saveType(Type type) {
        boolean save = typeService.save(type);
        return "redirect:/type/listType";
    }

    //跳转到修改页面,准备修改
    @RequestMapping("preUpdateType/{id}")
    public String preUpdateType(@PathVariable("id") Integer id, Model model) {
        Type byId = typeService.getById(id);
        model.addAttribute("type", byId);
        return "typeUpdate";
    }

    //修改类型
    @RequestMapping("updateType")
    public String updateType(Type type) {
        boolean b = typeService.updateById(type);
        return "redirect:/type/listType";
    }

    //删除
    @RequestMapping("delType/{id}")
    public String delType(@PathVariable("id") Integer id) {
        boolean b = typeService.removeById(id);
        return "redirect:/type/listType";
    }

    //查看图书
    @RequestMapping("bookList/{type}")
    public String bookList(@PathVariable("type") String type,
                           @RequestParam(required = false, defaultValue = "1", value = "pageNum") Integer pageNum,
                           @RequestParam(required = false, defaultValue = "10", value = "pageSize") Integer pageSize, HttpSession session, Model model, Book book) {
        if (pageNum <= 0 || pageNum.equals("") || pageNum == null) {
            pageNum = 1;
        }
        if (pageSize <= 0 || pageSize.equals("") || pageSize == null) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<Book> qw = new QueryWrapper<>();
        qw.like("type", type);
        if (book.getBname() != null) {
            qw.like("bname", book.getBname());
        }
        List<Book> list = bookService.list(qw);
        PageInfo<Book> pageInfo = new PageInfo<>(list);
        session.setAttribute("type", type);
        model.addAttribute("pageInfo", pageInfo);
        return "typeBookList";
    }

    //批量删除
    @PostMapping("batchDeleteType")
    @ResponseBody
    public String batchDeleteType(String idList) {
        String[] split = StrUtil.split(idList, ",");
        List<Integer> list = new ArrayList<>();

        for (String s : split) {
            if (!s.isEmpty()) {
                int i = Integer.parseInt(s);
                list.add(i);
            }
        }
        boolean b = typeService.removeByIds(list);
        if (b) {
            return "OK";
        } else {
            return "error";
        }
    }
}
